# -*-coding:utf-8 -*-
from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

#from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F
 
#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid
from myapp.myser import UserSer,PicsSer,GoodsSer,CommentSer


from myapp.models import *

# #定义 Redis 
# host = '127.0.0.1'
# port =6379
# #建立redis连接
# r = redis.Redis(host=host,port=port) 

# import pymongo
# #建立mongo的链接对象
# mongo_client = pymongo.MongoClient(host='localhost',port=27017)

# #导入bson
# from bson import json_util as jsonb


# #对结果集美化方法
# def dictfetchall(cursor):

#     #获取游标描述
#     desc = cursor.description
#     return [
#         dict(zip( [col[0] for col in desc ],row))
#         for row in cursor.fetchall()
#     ]

# #关注商品接口(取消)
# class GetFlow(APIView):
#     def get(self,request):
#         id = request.GET.get("id",None)
#         uid = request.GET.get("uid",None)
#         #默认1代表关注
#         type = request.GET.get("type",1)

#         if int(type) == 1:
#             #关注
#             userflow = UserFlow(gid=int(id),uid=int(uid))
#             try:
#                 userflow.save()
#                 r.set(str(userflow),123)
#                 r.expire(str(userflow),180)
#                 message = "关注成功"
#                 goods = Goods.objects.filter(id=int(id)).first()
#                 goods.flow = goods.flow +1
#                 goods.save()
#             except Exception  as e:
#                 print(str(e))
#                 message = "不能重复关注"
#                 pass
            
#         else:
#             #取关
#             user = UserFlow.objects.filter(gid=int(id),uid=int(uid)).delete()
            
#             message = "取关成功"
#             goods = Goods.objects.filter(id=int(id)).first()
#             goods.flow = goods.flow -1
#             goods.save()
#             #插入评论标记
#             r.set(str(user),123)
#             r.expire(str(user),180)
#         return Response({"message":message})


# class UidFlow(View):
#     def get(self,request):
#         uid = request.GET.get("uid",None)

#         #建立游标对象
#         cursor = connection.cursor()

#         #执行sql语句
#         cursor.execute('select a.name,a.create_time,a.id from goods a left join userflow b on a.id = b.gid where b.uid = %s '%str(uid) )

#         #获取结果 
#         # result = cursor.fetchall()
#         result = dictfetchall(cursor)

#         #返回结果 手动序列化
#         return HttpResponse(json.dumps(result,ensure_ascii = False,indent=4,default=str),content_type='application/json')


# #反序列化入库（商品评论）
# class InsertComment(APIView):  
#     def post(self,request):

#         print(request.data)
#         uid = request.POST.get("uid",None)
#         print(uid)
#         if r.get(str(uid)):
#             return Response({'code':403,'message':'你评论的太快'}) 

#         #初始化参数
#         comment = CommentSer(data=request.data)
#         #数据校验
#         if comment.is_valid():
#             #数据入库
#             # comment.create(comment.data)
#             comment.save()

#             #插入评论标记
#             r.set(str(uid),123)
#             r.expire(str(uid),30)
#         return Response({'code':200,'message':'恭喜评论成功'}) 